package com.noble.glamour.ui.discover;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.noble.glamour.R;
import com.noble.glamour.module.logic.baseui.BaseFragment;
import com.noble.glamour.ui.user.util.OffNetPanel;

/**
 * 发现页
 */
public class DiscoverMFragment extends BaseFragment {

    private FragmentManager fragmentManager;
    private BaseFragment current;
    private DiscoverFragment discoverFragment;      // 发现

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        setContentView(R.layout.discover_m_fragment);
        initView();
        initFragment();
        return getContentView();
    }

    private void initView() {
        setTitle(getString(R.string.discover_title));

        RelativeLayout netStatusLayout = (RelativeLayout) findViewById(R.id.netStatusLayout);
        OffNetPanel offNetPanel = new OffNetPanel(getActivity(), netStatusLayout);
        netStatusLayout.addView(offNetPanel);
    }

    private void initFragment() {
        fragmentManager = getChildFragmentManager();
        discoverFragment = new DiscoverFragment();
        switchContent(discoverFragment);
    }

    /**
     * 切换当前显示的fragment
     */
    private void switchContent(BaseFragment fragment) {
        if (current != fragment) {
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            if (current != null) {
                transaction.hide(current);
            }
            if (!fragment.isAdded()) { //先判断是否被add过
                transaction.add(R.id.discover_container, fragment).commitAllowingStateLoss();
            } else {
                transaction.show(fragment).commitAllowingStateLoss(); // 隐藏当前的fragment，显示下一个
            }
            current = fragment;
        }
    }
}
